package com.fs.myview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.studentmanagersystem.R;
import com.example.studentmanagersystem.URL;
import com.fs.po.StuSocre;
import com.fs.utils.CmdUtils;

public class UpdateGradeDiaolog implements OnClickListener {

	private Context context;
	private TextView dialogName,errorTv;
	private EditText dialogEditGrade,liuyanEt;
	private Button gradeSubmit,cancelBtn;
	private StuSocre stusocre;
	
	private AlertDialog alertDialog;
	private Handler handler;
	
	public UpdateGradeDiaolog(Context context,Handler handler) {
		
		this.context=context;
		this.handler=handler;

	}

	/** 
	 * 在代码中通过布局文件作为参数创建View 
	 */
	private View getView() {
		
		LayoutInflater inflater = ((Activity)context).getLayoutInflater();// 布局对象，专门操作布局文件的
		LinearLayout view = (LinearLayout) inflater.inflate(R.layout.update_grade_dialog, null);
		
		this.dialogName=(TextView) view.findViewById(R.id.update_grade_dialog_name);
		this.dialogEditGrade=(EditText) view.findViewById(R.id.update_grade_dialog_gradeET);
		this.gradeSubmit=(Button) view.findViewById(R.id.update_grade_dialog_submitBtn);
		this.cancelBtn=(Button) view.findViewById(R.id.update_grade_dialog_calButton);
		this.errorTv=(TextView) view.findViewById(R.id.update_grade_errorTextView);
		this.liuyanEt=(EditText) view.findViewById(R.id.update_grade_liuyan);
		
		this.dialogName.setText(stusocre.getName());
		
		this.gradeSubmit.setOnClickListener(this);
		this.cancelBtn.setOnClickListener(this);
		
		return view;
	}
	
	

	/**
	 * 设置监听器，当点击提交按钮时，就会提交所打分成绩到数据库
	 */
	@Override
	public void onClick(View v) {
		
		if(v.getId()==R.id.update_grade_dialog_submitBtn){//提交
			
			String newData=this.dialogEditGrade.getText().toString().trim();
			String pj=this.liuyanEt.getText().toString().trim();
			
			if(!"".equals(newData)){
				
				int data=Integer.parseInt(newData);
				
				String cmd="update data set score="+data+",pj='"+pj+"' where stu_id="+stusocre.getStu_id()+" and tec_id="+stusocre.getTec_id()+" and class_id="+stusocre.getClass_id()+"";
				
				CmdUtils cmdUtils=new CmdUtils(context);
				
				cmdUtils.submitCmd(URL.CMD_URL,"update",cmd);
				
				new Thread(){
					
					@Override
					public void run() {
						super.run();
						
						Message msg=Message.obtain();
						
						msg.what=1;
						handler.sendMessage(msg);
					}
					
				}.start();
				
				this.alertDialog.dismiss();
				
			}else if("".equals(newData)){
				
				this.errorTv.setError("请输入成绩！");
				 
			}
			
		}else if(v.getId()==R.id.update_grade_dialog_calButton){//取消
			
			 this.alertDialog.dismiss();
			
		}
		 
		
		
	}
	
	
	/**
	 * showDiaolog显示对话框,用于TeacherSetGradeActivity的调用
	 */
	public void showDialog(StuSocre stusocre) {
		
		this.stusocre=stusocre;
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		//设置属性
		builder.setView(this.getView());//设置对话框界面
		//创建对话框
		 alertDialog = builder.create();
		//显示对话框
		alertDialog.show();
		
	}
	
	
	
	
	
	
	
	
	
}
